﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;

using CooWork.Services;
using CooWork.Core;
using Coowork.Data.Mapping;

namespace CooWork.Models
{
    public class _DropDownModel
    {

        public List<SelectListItem> DropDownlst { get; set; }
 
        private int selectedID = -1;
        private string selectedString = "";
        private string name = "not set!";


        public string SelectedString
        {
            get
            {
                return selectedString;
            }
            set
            {
                selectedString = value;
            }
        }

        public int SelectedID
        {
            get
            {
                return selectedID;
            }
            set
            {
                selectedID = value;
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string indent(string s, int i)
        {
            while (i-- > 0)
            {
                s = "_" + s;
            }
            return s;
        }
    

        private void getDepartment(TreeNode tn, List<SelectListItem> lsli, int depth)
        {
            lsli.Add(new SelectListItem
            {
                Text=indent(tn.Tname,depth),
                Value=tn.Did.ToString(),
                Selected=tn.Did.ToString()==selectedString
            });
            if (tn.Count > 0)
            {
                depth++;
                foreach (TreeNode t in tn.Children)
                {
                    getDepartment(t, lsli,depth);
                }
            }
        }

        public void ddldepartment()
        {
            int depth = 0;
            TreeNode tn = UserService.GetDepartmentTree();
            DropDownlst = new List<SelectListItem>();
            getDepartment(tn,  DropDownlst, depth);


        }

        public void ddlroles()
        {
            var roles = Roles.GetAllRoles();
            DropDownlst = new List<SelectListItem>();
            foreach (string s in roles)
            {
                if (s == "超级管理员") continue;
                DropDownlst.Add(new SelectListItem
                {
                    Text = s,
                    Value = s,
                    Selected = s == selectedString

                });
            }
        }



        public void ddluser()
        {
           var allusers= Membership.GetAllUsers();
           DropDownlst = new List<SelectListItem>();
           foreach (MembershipUser mu in allusers)
           {
               DropDownlst.Add(new SelectListItem
               {
                   Text =mu.UserName,
                   Value =mu.ProviderUserKey.ToString(),
                   Selected = mu.ProviderUserKey.ToString() == selectedString

               });
           }
        }


        public void ddlStatus()
        {
            CooWorkEntities context = new CooWorkEntities();


            DropDownlst = context
                         .CooEnums.Where(a=>a.c=="StatusId")
                           .Select(a =>
                               new
                               {
                                   a.eid,
                                   a.ename
                               }
                               ).ToList()
                               .Select(a =>
                                   new SelectListItem
                                   {
                                       Text = a.ename.ToString(),
                                       Value = a.eid.ToString(),
                                       Selected = a.eid == selectedID

                                   }).ToList();
        }


        public void ddlpriority()
        {
            CooWorkEntities context = new CooWorkEntities();


            DropDownlst = context
                         .CooEnums.Where(a => a.c == "priority")
                           .Select(a =>
                               new
                               {
                                   a.eid,
                                   a.ename
                               }
                               ).ToList()
                               .Select(a =>
                                   new SelectListItem
                                   {
                                       Text = a.ename.ToString(),
                                       Value = a.eid.ToString(),
                                       Selected = a.eid == selectedID

                                   }).ToList();
        }


        public void ddlseverity()
        {
            CooWorkEntities context = new CooWorkEntities();


            DropDownlst = context
                         .CooEnums.Where(a => a.c == "severity")
                           .Select(a =>
                               new
                               {
                                   a.eid,
                                   a.ename
                               }
                               ).ToList()
                               .Select(a =>
                                   new SelectListItem
                                   {
                                       Text = a.ename.ToString(),
                                       Value = a.eid.ToString(),
                                       Selected = a.eid == selectedID

                                   }).ToList();
        }

        public void ddltask()
        {
          

           

            CooWorkEntities context = new CooWorkEntities();


            DropDownlst = context
                          .CooTasks
                           .Select(a =>
                               new
                               {
                                   a.ID
                                  
                               }
                               ).ToList()
                               .Select(a =>
                                   new SelectListItem
                                   {
                                       Text = a.ID.ToString(),
                                       Value = a.ID.ToString(),
                                       Selected = a.ID == selectedID

                                   }).ToList();
        }




        public void ddlist()
        {
            CooWorkEntities context = new CooWorkEntities();

          
            DropDownlst = context
                           .Departments
                           .Select(a =>
                               new
                               {
                                   a.ID,
                                   a.DepartmentName
                               }
                               ).ToList()
                               .Select(a =>
                                   new SelectListItem
                                   {
                                       Text = a.DepartmentName,
                                       Value = a.ID.ToString(),
                                       Selected = a.ID == selectedID

                                   }).ToList();
            

        }




    }
}